package components.plugins.duplicate
{
	import UIFree.ObjectEvent;
	import UIFree.components.uif_window;
	
	import classes.clt_events;
	import classes.clt_loader;
	import classes.clt_locale;
	
	import components.clt_arrowB;
	import components.dialogs.clt_dialog;
	import components.resultLayer.clt_personBitmap;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class clt_personage extends clt_dialog {
		private static const PERSONEPREFIX: String = 'persone';
		
		public function clt_personage(a_clt: clt_clothingBase) {
			super(clt.dialogLayer, 0, 0, 280, 80, false, uif_window.CLOSEHIDE);
		}
		
		override protected function createDialogComponents(): void {
			super.createDialogComponents();
			title.text = clt_locale.PERSONEMANAGER;
			var rect: Rectangle = getInner();
			rect.height = 30;
			new clt_arrowB(this, rect.x, rect.y, rect.width, rect.height, clt_locale.DUPLICATEPERSONE, doDuplicate);
			rect.y += rect.height;
			new clt_arrowB(this, rect.x, rect.y, rect.width, 30, clt_locale.CLEARPERSONE, doClear);
		}
		
		public function execute(a_params: Object): void {
			show();
		}
		
		private function doDuplicate(e: Event): void {
			clt.getResult(600, 600, doAfterCopy, {
				noLinkText: true,
				noback: true
			});
		}
		
		private function doClear(e: Event): void {
			var evObj: Object = {
				result: new Array()
			}
			
			workspace.dispatchEvent(new ObjectEvent(clt_events.GETELEMS, evObj));
			
			for each (var item: Object in evObj.result) {
				var na: Array = (item.name as String).split('_');
				if (na[0] == PERSONEPREFIX) 
					workspace.dispatchEvent(new ObjectEvent(clt_events.SETELEMENT, {
						name: item.name,
						bmp: null
					}));
			}
		}
		
		protected function doAfterCopy(bmd: BitmapData): void {
			clt.workspace.dispatchEvent(new ObjectEvent(clt_events.SETELEMENT, {
				name		: PERSONEPREFIX + '_' + clt.genFileNameCard(),
				bmp			: new Bitmap(bmd),
				elemClass	: clt_personBitmap,
				data		: {
					
				}
			}));	
			
			clt.alert(clt_locale.WARNING, 'Создан персонаж!\nОн находится сверху редактируемой фигуры.');
		}
	}
}